package com.rf.richfitwheel.workflow.service.impl;

import cn.avicnet.apigw.client.sdk.SignatureAuth;
import cn.avicnet.apigw.client.sdk.SignatureAuthConfig;
import cn.avicnet.apigw.client.sdk.SignatureAuthFactory;
import cn.avicnet.apigw.client.sdk.constant.ApiGwEnum;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Map;

/**
 * @author hushouquan
 */
public class HttpEntityJsonUtil{
    private static Gson gson = new Gson();
    private static JsonParser jp = new JsonParser();
    
    public static <T> T httpEntityContentToJsonArray(HttpEntity entity, Class<T> responseType) throws IOException{
        if (entity != null) {
            //先转为String
            String retSrc = EntityUtils.toString(entity);
            //转为JsonElement
            JsonElement je = jp.parse(retSrc);
            //通过JsonElement转为JsonObject
            JsonObject ja = je.getAsJsonObject();
            JsonObject content = ja.getAsJsonObject("content");
            return gson.fromJson(content, responseType);
        }else{
            return null;
        }
    }
    
    public static CloseableHttpClient getHttpsClient(boolean https){
        SSLContext sslContext = null;
        try {
            sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
                        @Override
                        public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException{
                            return true;
                        }
                    }).build();
        } catch (Exception e) {
            e.getStackTrace();
        }
        if(https){
            return HttpClients.custom().setSSLContext(sslContext).
                    setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
        }else{
            return HttpClients.createDefault();
        }
    }
    
    public static CloseableHttpResponse executeApi(String accessKey, String secretKey, String appId, String method, HttpEntity httpEntity, Map<String, String> headerMap, String url) throws URISyntaxException, IOException{
        SignatureAuthConfig signatureAuthConfig = new SignatureAuthConfig();
        signatureAuthConfig.setApiGw(ApiGwEnum.APACHE_APISIX);
        signatureAuthConfig.setAccessKey(accessKey);
        signatureAuthConfig.setSecretKey(secretKey);
        SignatureAuth signatureAuth = SignatureAuthFactory.createSignatureAuth(signatureAuthConfig);
        URI uri = new URIBuilder(url).build();
        HttpUriRequest httpUriRequest = signatureAuth.createHttpUriRequestBodySign(uri, method, httpEntity, headerMap);
        CloseableHttpClient httpClient = getHttpsClient(url.contains("https"));
        return httpClient.execute(httpUriRequest);
    }
}
